$(function() {
    
        $('#login-form-link').click(function(e) {
            $("#login-form").delay(100).fadeIn(100);
             $("#register-form").fadeOut(100);
            $('#register-form-link').removeClass('active');
            $(this).addClass('active');
            e.preventDefault();
        });
        $('#register-form-link').click(function(e) {
            $("#register-form").delay(100).fadeIn(100);
             $("#login-form").fadeOut(100);
            $('#login-form-link').removeClass('active');
            $(this).addClass('active');
            e.preventDefault();
        });

        //注册验证
        //用户名
        function checkusername(name){
            if(name.trim()  == ''){
                $($("#login-error-info").find("#name-error-info")).remove();
                $("#login-error-info").append("<p id='name-error-info'>用户名不能为空</p>")
             }else{
                 $($("#login-error-info").find("#name-error-info")).remove();
                 return true;
             }
             return false;
        }
        $("#register-form #username").blur(function(){
            var name =  $("#register-form #username").val();
            checkusername(name);
        })

        //邮箱
        function isEmail(str){
            var reg = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
            return reg.test(str);
        }
        function checkemail(email){
            if(email.trim()  == ''){
                $($("#login-error-info").find("#email-error-info")).remove();
                $("#login-error-info").append("<p id='email-error-info'>邮件不能为空</p>")
             }else if(!isEmail(email)){
                $($("#login-error-info").find("#email-error-info")).remove();
                 $("#login-error-info").append("<p id='email-error-info'>邮件格式不正确，请重新输入</p>")
             }
             else{
                $($("#login-error-info").find("#email-error-info")).remove();
                 return true;
             }
             return false;
        }
        $("#register-form #email").blur(function(){
            var email =  $("#register-form #email").val();
            checkemail(email);
        })
 
        //密码
        function checkpassword(password){
            if(password.trim() == "" || password.length < 6 ){
                $($("#login-error-info").find("#password-error-info")).remove();
                $("#login-error-info").append("<p id='password-error-info'>密码不能为空，切应该大于6位</p>")
            }else{
                $($("#login-error-info").find("#password-error-info")).remove();
                return true;
            }
            return false;
        }
        $("#register-form #password").blur(function(){
            var password =  $("#register-form #password").val();
            checkpassword(password);
        })

        //确认密码
        function checkc_password(c_password){
            var password =  $("#register-form #password").val();
            if(c_password != password){
                $($("#login-error-info").find("#cpassword-error-info")).remove();
                $("#login-error-info").append("<p id='cpassword-error-info'>确认密码应该和上面的密码一支</p>")
            }
            else{
                $($("#login-error-info").find("#cpassword-error-info")).remove();
                return true;
            }
            return false;
        }

        $("#register-form #confirm-password").blur(function(){
            var c_password =  $("#register-form #confirm-password").val();
            checkc_password(c_password);
        })
        
        //

        //注册按钮点击
        $('#register-submit').click(function(){

            var username = $("#register-form #username").val();
            var email = $("#register-form #email").val();
            var password = $("#register-form #password").val();
            var c_password = $("#register-form #confirm-password").val();

            if(checkusername(username) && checkemail(email) && checkpassword(password) && checkc_password(c_password)){
                var registerinfo = {
                    "username" : username,
                    "email" : email,
                    "password" : password,
                }
                $.ajax({
                    url: serviceInfo.ip +"register",
                    type:"post",
                    data:JSON.stringify(registerinfo),
                    contentType:"application/json",
                    success:function(res){
                        if(res.result == "true"){
                            Login(username);
                            window.location.href = "./index.html";
                        }else{
                            alert(username + "已经存在！ ");
                        }
                    },
                    error:function(e){
                        alert("错误！！");
                    }
             
                }); 
            }
        })


        $("#login-submit").click(function(){
            var name = $("#login-form #username").val();
            var password = $("#login-form #password").val();
            var logininfo = {
                "username" : name,
                "password" : password
            };

            $.ajax({
                url : serviceInfo.ip +"dologin",
                type : "post",
                data : JSON.stringify(logininfo),
                dataType : "json",
                contentType:"application/json",
                success : function(res){
                    if(res.result == "true"){
                        Login(name);
                        window.location.href = "./index.html";
                    }else{
                        alert(res.errorMeg);
                    }
                },
                error: function(xml,mes){
                    alert(mes);
                }
            })

        });
    });

            